﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace TST.SharePointObjects.WebControls
{
    public class SortGrid : WebControl
    {
        private SPGridView _grid;

        public SortGrid()
        {
        }

        protected override void CreateChildControls()
        {
            _grid = new SPGridView();
            _grid.AutoGenerateColumns =false;
            _grid.AllowSorting = true;
            _grid.EnableViewState = false;
            _grid.ID = "SortGrid_" + this.ID;
            _grid.Width = Unit.Percentage(100);
            if (!DisableSorting)
            {
                _grid.Sorting += new System.Web.UI.WebControls.GridViewSortEventHandler(SortGrid_Sorting);
            }
            else
            {
                _grid.RowDataBound += new GridViewRowEventHandler(SharePointGrid_RowDataBound);
            }
            Controls.Add(_grid);
            base.CreateChildControls();
        }

        public Boolean DisableSorting
        {
            get;
            set;
        }

        public SPGridView Grid
        {
            get
            {
                EnsureChildControls();
                return _grid;
            }
        }

        public string GridSortExpression
        {
            get
            {
                if (ViewState["SortExpression"] != null)
                {
                    return ViewState["SortExpression"].ToString();
                }
                return string.Empty;
            }
            set
            {
                ViewState["SortExpression"] = value;
            }
        }

        public SortDirection GridSortDirection
        {
            get
            {
                if (ViewState["SortDirection"] != null)
                {
                    return (SortDirection)ViewState["SortDirection"];
                }
                return SortDirection.Ascending;
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }

        public string SelectedKey
        {
            get
            {
                if (ViewState["SelectedKey"] != null)
                {
                    return ViewState["SelectedKey"].ToString();
                }
                return string.Empty;
            }
            set
            {
                ViewState["SelectedKey"] = value;
            }
        }

        private void SortGrid_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
        {
            string lastExpression = GridSortExpression;
            SortDirection lastDirection = GridSortDirection;

            SortDirection newDirection = SortDirection.Ascending;
            if (e.SortExpression == lastExpression)
                newDirection = (lastDirection == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending;

            GridSortExpression = e.SortExpression;
            GridSortDirection = newDirection;
            Grid.SelectedIndex = -1;
        }

        private void SharePointGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (sender == null || e.Row.RowType != DataControlRowType.Header)
            {
                return;
            }

            SPGridView grid = sender as SPGridView;

            if (String.IsNullOrEmpty(grid.FilterFieldName))
            {
                return;
            }

            // Show icon on filtered column
            for (int i = 0; i < grid.Columns.Count; i++)
            {
                DataControlField field = grid.Columns[i];

                if (field.SortExpression == grid.FilterFieldName)
                {
                    Image filterIcon = new Image();
                    filterIcon.ImageUrl = "/_layouts/images/filter.gif";
                    filterIcon.Style[HtmlTextWriterStyle.MarginLeft] = "2px";

                    Literal headerText = new Literal();
                    headerText.Text = field.HeaderText;

                    PlaceHolder panel = new PlaceHolder();
                    panel.Controls.Add(headerText);
                    panel.Controls.Add(filterIcon);

                    e.Row.Cells[i].Controls[0].Controls.Add(panel);

                    break;
                }
            }
        }
    }
}
